<?php
/**
 * Minhlog 数据库验证类
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Minh
 * @package    Minh_Auth
 * @copyright  Copyright (c) 2009-2010 Minh.Shu Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: Db.php 32 2010-03-30 12:40:08Z minh.shu $
 *
 */
class Minh_Auth_Db
{
    /**
     * Zend_Auth实例
     * @var Zend_Auth
     */
    private $_auth = null;
    /**
     * 持久化存储句柄
     * @var Zend_Auth_Storage_Interface
     */
    private $_storage = null;
    /**
     * Zend_Auth_Adapter_DbTable实例
     * @var Zend_Auth_Adapter_DbTable
     */
    private $_authAdapter = null;
    /**
     * 构造函数
     * @access public
     * @return void
     */
    public function __construct ()
    {
        $bootstrap = Zend_Controller_Front::getInstance()->getParam(
        'bootstrap');
        $resource = $bootstrap->getPluginResource(
        'db');
        $db = $resource->getDbAdapter();
        $this->_auth = Zend_Auth::getInstance();
        $this->_auth->setStorage(
        new Zend_Auth_Storage_Session('Minhlog'));
        $this->_storage = $this->_auth->getStorage();
        $this->_authAdapter = new Zend_Auth_Adapter_DbTable(
        $db);
        $this->_authAdapter->setTableName(
        'mlog_user')->setIdentityColumn(
        'username')->setCredentialColumn(
        'password');
    }
    /**
     * 验证用户信息
     * @param string $username
     * @param string $password
     * @return object
     */
    public function authUser ($username, $password)
    {
        $this->_authAdapter->setIdentity(
        $username);
        $this->_authAdapter->setCredential(
        $password);
        $authResult = $this->_auth->authenticate(
        $this->_authAdapter);
        return $authResult;
    }
    /**
     * 认证持久化
     * @return
     */
    public function authPersist ()
    {
        return $this->_storage->write(
        $this->_authAdapter->getResultRowObject(
        null, 'password'));
    }
    /**
     * 获取验证信息
     * @return
     */
    public function getAuthInfo ()
    {
        return $this->_storage->read();
    }
}
